package cn.gameback.supervisionservices.mapper;

import cn.gameback.supervisionservices.dto.User.LeaderCrewDTO;
import cn.gameback.supervisionservices.entity.LeaderCrew;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface LeaderCrewMapper extends BaseMapper<LeaderCrew> {

    /**
     * 根据多个下级用户ID查找所有上级的ID。
     * @param userIds 用户ID列表
     * @return 上级用户ID列表
     */
    @Select({
            "<script>",
            "SELECT crewId AS userId, leaderId FROM leader_crew",
            "WHERE crewId IN",
            "<foreach item='id' collection='userIds' open='(' separator=',' close=')'>",
            "#{id}",
            "</foreach>",
            "</script>"
    })
    List<LeaderCrewDTO> findLeaderIdsByUserIds(@Param("userIds") List<Integer> userIds);

    /**
     * 根据下级用户ID获取对应的上级用户ID。
     * @param userId 下级用户ID
     * @return 上级用户ID
     */
    @Select("SELECT crewId FROM leader_crew WHERE leaderId = #{userId}")
    List<Integer> getLeaderIdByUserId(@Param("userId") Integer userId);

    /**
     * 删除对应下级的数据
     * @param crewId
     * @return
     */
    @Delete("DELETE FROM leader_crew WHERE crewId = #{crewId}")
    int deleteByMemberId(Integer crewId);

    // 添加下级的上级用户
    @Insert({
            "<script>",
            "INSERT INTO leader_crew (crewId, leaderId)",
            "VALUES",
            "<foreach collection=\"leaderId\" item=\"leaderId\" separator=\",\">",
            "(#{crewId}, #{leaderId})",
            "</foreach>",
            "</script>"
    })
    int batchInsert(
            @Param("crewId") Integer crewId,
            @Param("leaderId") List<Integer> leaderId
    );
}